package cn.adelyn.blog.file.test;

import cn.adelyn.base.api.auth.pojo.bo.UserInfoInTokenBO;
import cn.adelyn.base.api.snowfalke.feign.SnowflakeFeignClient;
import cn.adelyn.base.secureweb.AuthUserContext;
import cn.adelyn.blog.file.constant.MinioConstant;
import cn.adelyn.blog.file.pojo.dto.InsertFileInfoDTO;
import cn.adelyn.blog.file.pojo.dto.InsertShareInfoDTO;
import cn.adelyn.blog.file.service.FileService;
import cn.adelyn.blog.file.service.ShareService;
import cn.adelyn.common.oss.config.MinioTemplate;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.Date;

@Slf4j
//@SpringBootTest(classes = FileApplication.class)
public class FileTester {

    @Autowired
    MinioTemplate minioTemplate;

    @Autowired
    SnowflakeFeignClient snowflakeFeignClient;

    @Autowired
    FileService fileService;

    @Autowired
    ShareService shareService;

    @Test
    void getDownloadUrl() throws Exception {
        Long picId = 1627591590463803392L;
        String downloadUrl = minioTemplate.genDownloadUrl(MinioConstant.BUCKET, MinioConstant.PIC_PATH_PREFIX + MinioConstant.PATH_SEPARATOR + picId,
                180, MinioConstant.TIME_UNIT);
        log.info("pic downloadUrl: {}", downloadUrl);
    }

    @Test
    void getUploadUrl() throws Exception {
        Long picId = snowflakeFeignClient.snowflakeId().getData();
        String uploadUrl =  minioTemplate.genUploadUrl(MinioConstant.BUCKET, MinioConstant.PIC_PATH_PREFIX + MinioConstant.PATH_SEPARATOR + picId,
                180, MinioConstant.TIME_UNIT);
        log.info("pic uploadUrl: {}", uploadUrl);
    }

    @Test
    void insertFileInfo() {
        InsertFileInfoDTO insertFileInfoDTO = new InsertFileInfoDTO();
        insertFileInfoDTO.setFileId(213123L);
        insertFileInfoDTO.setFileName("测试文件");
        fileService.insertFileInfo(insertFileInfoDTO, 2L);
    }

    @Test
    void insertShareInfo() {
        InsertShareInfoDTO insertShareInfoDTO = new InsertShareInfoDTO();
        insertShareInfoDTO.setFileId(213123L);
        insertShareInfoDTO.setInvalidTime(new Date(2023-1900, 2-1, 23));
        UserInfoInTokenBO userInfoInTokenBO = new UserInfoInTokenBO();
        userInfoInTokenBO.setUserId(2L);
        userInfoInTokenBO.setUserType(1);
        AuthUserContext.set(userInfoInTokenBO);
        shareService.insertShareInfo(insertShareInfoDTO);
    }

    @Test
    void getShareFileDownloadUrl() {
        log.info(shareService.genSharedFileDownloadUrl(1627869291925868544L));
    }
}
